/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isip.transformation;

import com.azon.ip.utils.ImageReader;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class LookUpTableMaker implements Transformation {
    
    public LookUpTableMaker() {
    
    }
    
    public int[][] doMakeLookUpTable(int[][] matrix) {
        int[] histogram = ImageReader.ReadHistogramInGrayScale(matrix);
        int min = 0;
        int max = histogram.length-1;
        
        while(histogram[min]==0) {
            min++;
        }
        
        while(histogram[max]==0) {
            max--;
        }
        
        int[] frequency = new int[max - min + 1];
        
        frequency[0] = 0;
        
        for (int i = 1; i < frequency.length; i++) {
            frequency[i] = histogram[i + min] + frequency[i-1];
        }
        
        /*
        for (int i=min; i < max + 1; i++) {
            System.out.println("nilai ke-" + i + " = " + histogram[i]);
        } // */
        
        /*
        for (int i=0; i < frequency.length; i++) {
            System.out.println("frekuensi ke-" + i + " (" + (min + i) + ") = " + frequency[i]);
        } // */
        
        /*
        System.out.println("Minimal = " + min);
        System.out.println("maximal = " + max); // */
        
        int[][] lookUpTable = new int[frequency.length][2];
        
        lookUpTable[0][0] = min;
        lookUpTable[0][1] = 0;
        
        
        for (int i = 1; i < frequency.length; i++) {
            lookUpTable[i][0] = min + i;
            lookUpTable[i][1] = (int)((float)frequency[i] / (float)frequency[frequency.length - 1] * 255);
        } // */
        
        /*
        for (int i = 0; i < frequency.length; i++) {
            System.out.println(lookUpTable[i][0] + " --> " + lookUpTable[i][1]);
        } // */
        
        return lookUpTable;
    }
    
    public int[][] useLookUpTable(int[][] matrix, int[][] lookUpTable) {
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
        /*
        for (int i = 0; i < lookUpTable.length; i++) {
            System.out.println(lookUpTable[i][0] + " --> " + lookUpTable[i][1]);
        } // */
        
        for (int m = 0; m < matrix.length; m++) {
            for (int n = 0; n < matrix[0].length; n++) {
                for (int i = 0; i < lookUpTable.length; i++) {
                    if (matrix[m][n] == lookUpTable[i][0]) {
                        new_matrix[m][n] = lookUpTable[i][1];
                        break;
                    }
                }
            }
        }
        
        return new_matrix;
    }

    @Override
    public int[][] doTransform(int[][] matrix) {
        int[][] lookUpTable = doMakeLookUpTable(matrix);
        
        int[][] new_matrix = useLookUpTable(matrix, lookUpTable);
        
        return new_matrix;
    }

}
